﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Wormhole.UI.Application.Dialogs.JoinGame
{
	/// <author>Hannes Foulds</author>
	/// <date>25 September 2008</date>
	/// <summary>
	/// Interaction logic for Wait.xaml
	/// </summary>
	public partial class Wait : Window
	{
		#region Declarations
		/// <summary>
		/// The timer used to update the progress bar.
		/// </summary>
		private System.Windows.Threading.DispatcherTimer timer;
		#endregion

		#region Properties
		/// <summary>
		/// Is the user allowed to close the dialog box.
		/// </summary>
		public bool Done { get; set; }
		#endregion

		#region Constructor
		/// <summary>
		/// Initialze a default instace of the class.
		/// </summary>
		public Wait()
		{
			InitializeComponent();

			// create the timer
			this.timer = new System.Windows.Threading.DispatcherTimer();
			this.timer.Interval = TimeSpan.FromMilliseconds(30);
			this.timer.IsEnabled = true;
			this.timer.Tick += new EventHandler(timer_Tick);
		}
		#endregion

		#region Event Handlers
		
		#region Timer Tick
		private void timer_Tick(object sender, EventArgs e)
		{
			this.progressBar.Value++;

			if (this.progressBar.Value == this.progressBar.Maximum)
				this.progressBar.Value = this.progressBar.Minimum;
		}
		#endregion

		#endregion

		#region On Closing
		/// <summary>
		/// Only allow the dialog to be closed when done.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			if (!this.Done)
				e.Cancel = true;
			else
				this.timer.IsEnabled = false;

			base.OnClosing(e);
		}
		#endregion
	}
}
